
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include( 'shared.lua' )

function ENT:Initialize()

	self.Entity:SetModel( "models/\props_phx/amraam.mdl" )
	self.Entity:SetName("Artillery Shell")
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_VPHYSICS )

	local phys = self.Entity:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:Wake()
		phys:EnableGravity(true)
		phys:EnableDrag(true)
		phys:EnableCollisions(true)
	end

    	self.Entity:SetKeyValue("rendercolor", "255 255 255")
	self.PhysObj = self.Entity:GetPhysicsObject()
	self.PhysObj:SetMass( 30 )


end

function ENT:SpawnFunction( ply, tr )

	if ( !tr.Hit ) then return end
	
	local SpawnPos = tr.HitPos + tr.HitNormal * 16
	
	local ent = ents.Create( "forocket" )
	ent:SetPos( SpawnPos )
	ent:Spawn()
	ent:Activate()
	ent.SPL = ply
	ent.VPl = ply
	
	return ent
	
end

function ENT:PhysicsUpdate()

	if(self.Exploded) then
		self.Entity:Remove()
		return
	end

end

function ENT:Think()
	if (self.Fired == true) then
		if (self.PreLaunch == false) then
			self.PreLaunch = true
			local phys = self.Entity:GetPhysicsObject()
			if (phys:IsValid()) then
				phys:Wake()
				phys:EnableGravity(false)
				phys:EnableDrag(true)
				phys:EnableCollisions(true)
				phys:EnableMotion(true)
			end
			self.PhysObj:AddVelocity( self.Entity:GetUp() * -50 )
		
	
			RockTrail = ents.Create("env_fire_trail")
			RockTrail:SetAngles( self.Entity:GetAngles()  )
			RockTrail:SetPos( self.Entity:GetPos() + (self.Entity:GetForward() * -50) )
	
			RockTrail:SetParent(self.Entity)
			RockTrail:Spawn()
			RockTrail:Activate()
			
			self.AimP = self.Entity:GetAngles()

			if (self.VPl != nil) then
				if (self.VPl.MSTr == nil) then
					self.VPl.MSTr = self.Entity
					self.VPl:SetViewEntity(self.Entity)
					self.Tracking = true
				end
			end
			
			self.PreLaunch = true
		end
		if (self.Tracking == true) then
			self.Entity:SetAngles( self.VPl:GetAimVector():Angle() )
		else
			self.Entity:SetAngles( self.AimP )
		end
		self.PhysObj:AddVelocity( self.Entity:GetForward() * 100000 )
		
		if (self.VPl:KeyDown( IN_ATTACK ) == true && self.Tracking == true) then
			if(!self.Exploded) then
				self.Exploded = true
				if (self.Warhead == "") then				
					util.BlastDamage(self.Entity, self.Entity, self.Entity:GetPos(), 300, 50)
					cbt_hcgexplode( self.Entity:GetPos(), 150, 300, 2)
		 
					local effectdata = EffectData()
					effectdata:SetOrigin(self.Entity:GetPos())
					effectdata:SetStart(self.Entity:GetPos())
					util.Effect( "big_splosion", effectdata )
				else
					local WHead = ents.Create( self.Warhead )
					WHead:SetPos( self.Entity:GetPos() )
					WHead:SetAngles( self.Entity:GetAngles() )
					WHead:SetOwner( self.SPL )
					WHead:Spawn()
					WHead:Activate()
				end
			end
		end
	end

	self.Entity:NextThink( CurTime() + 0.01 ) 
	return true
end

function ENT:PhysicsCollide( data, physobj )
	if (self.Fired == true) then
		if(!self.Exploded) then
			self.Exploded = true
			if (self.Warhead == "") then				
				util.BlastDamage(self.Entity, self.Entity, self.Entity:GetPos(), 300, 50)
				cbt_hcgexplode( self.Entity:GetPos(), 150, 300, 2)
	 
				local effectdata = EffectData()
				effectdata:SetOrigin(self.Entity:GetPos())
				effectdata:SetStart(self.Entity:GetPos())
				util.Effect( "big_splosion", effectdata )
			else
				local WHead = ents.Create( self.Warhead )
				WHead:SetPos( self.Entity:GetPos() )
				WHead:SetAngles( self.Entity:GetAngles() )
				WHead:SetOwner( self.SPL )
				WHead:Spawn()
				WHead:Activate()
			end
		end
	end
end

function ENT:OnTakeDamage( dmginfo )
	if (self.Fired == true) then
		if(!self.Exploded) then
			local expl=ents.Create("env_explosion")
			expl:SetPos(self.Entity:GetPos())
			expl:SetName("Missile")
			expl:SetParent(self.Entity)
			expl:SetOwner(self.SPL)
			expl:SetKeyValue("iMagnitude","100");
			expl:SetKeyValue("iRadiusOverride", 150)
			expl:Spawn()
			expl:Activate()
			expl:Fire("explode", "", 0)
			expl:Fire("kill","",0)
			self.Exploded = true
		end	
	end
end

function ENT:Use( activator, caller )
	self.VPl = activator
end

function ENT:OnRemove()
	if (self.VPl != nil) then
		if (self.VPl.MSTr == self.Entity) then
			self.VPl.MSTr = nil
			self.VPl:SetViewEntity(self.VPl)
		end
	end
end